package com.cljz.makepassenger.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cljz.makepassenger.R;

/**
 * Created by changlianjiuzhou on 17/2/28.
 */

public class TimeButton extends LinearLayout {
    private ImageView img;
    private TextView name;
    public boolean isCheck = false;

    public TimeButton(Context context) {
        super(context);
        initView(context, null);
    }

    public TimeButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public void setName(String name) {
        this.name.setText(name);
    }

    public TimeButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        LayoutInflater.from(getContext()).inflate(R.layout.time_button_layout, this);
        img = (ImageView) findViewById(R.id.time_button_img);
        name = (TextView) findViewById(R.id.time_button_name);
        parseStyle(context, attrs);
    }

    private void parseStyle(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TimeButton);
            String title = ta.getString(R.styleable.TimeButton_buttontitle);
            name.setText(title);

            Drawable leftDrawable = ta.getDrawable(R.styleable.TimeButton_leftimg);
            if (null != leftDrawable) {
                img.setImageDrawable(leftDrawable);
            }

            ta.recycle();
        }
    }


    public void setCheck(boolean check) {
        this.isCheck = check;
        name.setTextColor(check ? getResources().getColor(R.color.time_select_color) :getClickable()?getResources().getColor(R.color.text_color):getResources().getColor(R.color.grey_500));
    }

    public boolean getClickable() {
        return isClickable();
    }



    public void setCheckDrawable(int checkId) {
        img.setImageResource(checkId);
    }

    public void setSingleTextColor(int color){
       name.setTextColor(color);
    }
    public void setClick(boolean is, int resId) {
        setClickable(is);
        name.setTextColor(is ? getResources().getColor(R.color.text_color) : getResources().getColor(R.color.grey_500));
        img.setImageResource(resId);
    }
}
